import {Component, ElementRef, Input, ViewChild} from '@angular/core';
import {CommonPage} from '../common-page';
import {DhlTreeComponent} from '../../../shared/components/dhlcomponent/tree/dhl-tree.component';
import {MenuFormComponent} from './menuForm/menu.form.component';
import {ObjectUtil} from '../../../shared/utils/object-util';
import {FormUtil} from '../../../shared/utils/form-util';
import {ActionFormComponent} from './actionForm/action.form.component';

@Component({
  templateUrl: './menu.component.html'
})
export class MenuComponent extends CommonPage {


  showMenu = true;
  menuValue;
  checkedMenuNodeId;
  checkedMenuNodeName = 'root';
  checkedMenuNodeCode = 'root';
  menuConfirm;
  actionConfirm;
  actionCheckedId = 0;
  actionBtnDisable = true;
  menuBtnDisable = false;
  menuSaveBtnDisable = true;
  actionSaveBtnDisable = true;
  projectIdNum;

  @Input() menuRootCode;

  @Input() set projectId(value) {
    this.projectIdNum = value;
  }


  @ViewChild('menuTree', {read: DhlTreeComponent}) menuTree: DhlTreeComponent;
  @ViewChild('menuForm', {read: MenuFormComponent}) menuForm: MenuFormComponent;
  @ViewChild('menuForm', {read: ElementRef}) menuFormRef: ElementRef;
  @ViewChild('actionForm', {read: ElementRef}) actionFormRef: ElementRef;
  @ViewChild('actionForm', {read: ActionFormComponent}) actionForm: ActionFormComponent;


  addMenu() {
    this.windowService.openWindow({
      submitUrl: `menu/save`,
      data: {
        parentId: this.checkedMenuNodeId,
        parentName: this.checkedMenuNodeName,
        parentCode: this.checkedMenuNodeCode,
        projectId: this.projectIdNum
      },
      component: MenuFormComponent,
      title: '新增菜单',
      afterOk: () => {
        this.freshMenu();
      }
    });
  }

  addAction() {
    this.windowService.openWindow({
      submitUrl: `action/saveaction`,
      data: {
        menuNodeName: this.checkedMenuNodeName,
        checkedMenuId: this.checkedMenuNodeId
      },
      component: ActionFormComponent,
      title: '新增权限',
      afterOk: () => {
        this.freshMenu();
      }
    });
  }

  nodeChangeClick(value) {

    if (ObjectUtil.isHaveValue(value) && value.length > 0) {
      if (value[0].action) {
        this.showMenu = false;
        this.actionBtnDisable = true;
        this.menuBtnDisable = true;
        this.actionSaveBtnDisable = false;
        this.actionChangeClick(value);
      } else {
        if (value[0].childrenData && value[0].childrenData.length > 0 && value[0].childrenData[0].action) {
          this.menuBtnDisable = true;
        } else {
          this.menuBtnDisable = false;
        }
        this.actionCheckedId = 0;
        this.showMenu = true;
        this.actionBtnDisable = false;
        this.menuSaveBtnDisable = false;
        this.menuChangeClick(value);
      }
    } else {
      if (this.menuFormRef) {
        FormUtil.reset($(this.menuFormRef.nativeElement));
        this.menuForm.id = undefined;
        this.menuForm.projectId = undefined;
        this.menuForm.parentName = '';
        this.menuSaveBtnDisable = true;
      }
      if (this.actionFormRef) {
        this.actionCheckedId = 0;
        this.actionForm.id = undefined;
        this.actionForm.projectId = undefined;
        this.actionForm.inputUrl = undefined;
        this.actionForm.menuNodeName = '';
        this.actionForm.name = '';
        this.actionForm.code = '';
        this.actionSaveBtnDisable = true;
      }
      this.checkedMenuNodeName = 'root';
      this.checkedMenuNodeCode = 'root';
      this.checkedMenuNodeId = undefined;
    }

  }

  menuChangeClick(value) {

    const parentNode = value[0].fatherLine.length > 1 ? value[0].fatherLine[1] : null;
    this.checkedMenuNodeId = value[0].id;
    this.checkedMenuNodeName = value[0].title;
    this.checkedMenuNodeCode = value[0].key;
    const actions = [];
    if (value[0].childrenData && value[0].childrenData.length > 0) {
      const isAction = value[0].childrenData[0].action;
      if (isAction) {
        for (const child of value[0].childrenData) {
          const codeArray = child.code.split('_');
          actions.push(codeArray[codeArray.length - 1]);
        }
      }
    }

    this.menuConfirm = `确认删除"${this.checkedMenuNodeName}"菜单吗?`;

    setTimeout(() => {
      const menu = value[0].menu || {};

      this.menuForm.id = menu.id || '';
      this.menuForm.name = menu.name || '';
      this.menuForm.code = menu.code || '';
      this.menuForm.icon = menu.icon || '';
      this.menuForm.clickUrl = menu.clickUrl || '';
      this.menuForm.actionValue = actions;

      this.menuForm.projectId = this.projectIdNum;
      this.menuForm.nodeName = value[0].title;
      this.menuForm.nodeCode = value[0].key;
      this.menuForm.nodeId = value[0].id;

      this.menuForm.parentName = parentNode ? parentNode.title : 'root';
      this.menuForm.parentCode = parentNode ? parentNode.key : 'root';
      this.menuForm.parentId = parentNode ? parentNode.id : null;
    }, 100);


  }

  actionChangeClick(value) {

    let actionValue = value[0];
    let menuName;
    let menuId;
    let actionName;
    const fatherLine = actionValue.fatherLine;
    if (fatherLine[1].menu) {
      menuName = fatherLine[1].title;
      menuId = fatherLine[1].id;
      actionName = fatherLine[0].title;
    } else {
      menuName = fatherLine[2].title;
      menuId = fatherLine[2].id;
      actionName = fatherLine[1].title;
      actionValue = fatherLine[1];
    }

    this.actionConfirm = `确认删除"${actionName}"权限吗?`;

    if (this.actionCheckedId != actionValue.id) {
      this.actionCheckedId = actionValue.id;
      setTimeout(() => {
        const action = actionValue.action || {};

        this.actionForm.id = action.id || undefined;
        this.actionForm.name = action.name || '';
        this.actionForm.code = action.code || '';
        this.actionForm.inputUrl = action.url ? action.url.split(',') : null;

        this.actionForm.projectId = this.projectIdNum;
        this.actionForm.checkedMenuId = menuId;
        this.actionForm.nodeId = actionValue.id;
        this.actionForm.menuNodeName = menuName;
      }, 100);
    }

  }


  deleteMenuClick() {
    this.requestService.post(`tree/delete/${this.menuForm.nodeId}`).subscribe(data => {
      if (data) {
        FormUtil.reset($(this.menuFormRef.nativeElement));
        this.freshMenu();
      }
    });
  }

  deleteActionClick() {
    this.requestService.post(`tree/delete/${this.actionForm.nodeId}`).subscribe(data => {
      if (data) {
        FormUtil.reset($(this.actionFormRef.nativeElement));
        this.freshMenu();
      }
    });
  }

  freshMenu() {
    this.menuTree.fresh();
  }

  saveMenu() {
    this.messageService.success('修改成功');
    this.freshMenu();
  }

}




